/*
 * @Author: your name
 * @Date: 2021-07-21 15:02:19
 * @LastEditTime: 2021-07-26 16:09:17
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \geek-client-mobile\src\api\article.js
 */
// 提供文章相关的接口API函数
import request from '@/utils/request'
/**
 * 根据频道获取文章列表
 * @param {Number} channelId - 频道ID
 * @param {Number} timestamp - 时间戳
 * @returns
 */
export const getArticlesByChannel = (channelId, timestamp) => {
  return request({
    url: '/v1_0/articles',
    params: { channel_id: channelId, timestamp }
  })
}

/**
 * 获取文章详情
 * @param{String}id - 文章ID
 * @returns
 */
export const getArticle = (articleId) => {
  return request({
    url: '/v1_0/articles/' + articleId
  })
}

/**
 * 获取文章评论
 * @param {String} articleId - 文章ID
 * @param {String} offset - 偏移量，第一页不传，其他页：上一次的last_id作为偏移量
 * @returns
 */
export const getCommentsByArticle = (articleId, offset) => {
  return request({
    url: 'v1_0/comments',
    params: { type: 'a', source: articleId, offset }
  })
}

/**
 *获取评论的回复
 * @param {String} commentId -评论的ID
 * @param {String} offset -偏移量，第一页不传，其他页：上一次的last——id作为偏移量
 * @returns
 */
export const getReplysByComment = (commentId, offset) => {
  return request({
    url: '/v1_0/comments',
    params: { type: 'c', source: commentId, offset }
  })
}

/**
 * 对文章进行点赞或者取消点赞
 * @param {String} articleId -文章ID
 * @param {Boolean} isLike -对文章的状态，点赞 取消点赞
 */
export const likeArticle = (articleId, isLike) => {
  if (isLike) {
    // 进行点赞
    return request({
      url: '/v1_0/article/likings',
      method: 'post',
      data: { target: articleId }
    })
  } else {
    // 取消点赞
    return request({
      url: '/v1_0/article/likings/' + articleId,
      method: 'delete'
    })
  }
}

/**
 * 收藏文章，取消收藏文章
 * @param {String} article -文章ID
 * @param {Boolean} isCollect -收藏，取消收藏
 */
export const collectArticle = (articleId, isCollect) => {
  if (isCollect) {
    // 收藏
    return request({
      url: '/v1_0/article/collections',
      method: 'post',
      data: { target: articleId }
    })
  } else {
    // 取消
    return request({
      url: '/v1_0/article/collections/' + articleId,
      method: 'delete'
    })
  }
}

/**
 *对文章评论，对评论回复
 * @param {String} target -文章Id，评论ID
 * @param {String} articleId -评论或者回复的内容
 * @param {String} content -当你是回复的时候，需要传文章的ID
 * @returns
 */
export const commentOrReply = (target, content, articleId = null) => {
  return request({
    url: '/v1_0/comments',
    method: 'post',
    data: { target, content, art_id: articleId }
  })
}
